package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.badge.BadgeConfig;
import com.yanqu.road.entity.enums.eBadgeType;
import com.yanqu.road.logic.bussiness.config.BadgeConfigBusiness;
import com.yanqu.road.server.TempMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BadgeConfigMgr extends TempMgr {

    // 全部勋章配置    key：勋章类型，勋章ID
    private static Map<Integer, Map<Integer, BadgeConfig>> badgeConfigMap = new ConcurrentHashMap<>();

    private static final Map<Integer, BadgeConfig> wineryBadgeConfigMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        List<BadgeConfig> configList = BadgeConfigBusiness.getConfigList();
        for (BadgeConfig config : configList) {
            //type = 3，酒坊徽章
            if (config.getType() == eBadgeType.Winery.getValue()) {
                wineryBadgeConfigMap.put(config.getLevel(), config);
            }
        }

        Map<Integer, Map<Integer, BadgeConfig>> tmpConfig = new ConcurrentHashMap<>();
        for (BadgeConfig badgeConfig : configList) {
            tmpConfig.putIfAbsent(badgeConfig.getType(), new ConcurrentHashMap<>());
            tmpConfig.get(badgeConfig.getType()).put(badgeConfig.getId(), badgeConfig);
        }
        badgeConfigMap = tmpConfig;

        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static BadgeConfig getWineryBadgeConfig(int level) {
        return BadgeConfigMgr.wineryBadgeConfigMap.get(level);
    }

    public static BadgeConfig getBadgeConfig(int badgeType, int badgeId) {
        Map<Integer, BadgeConfig> map = badgeConfigMap.get(badgeType);
        if (map == null) {
            return null;
        }
        return map.get(badgeId);
    }

    public static Map<Integer, BadgeConfig> getBadgeConfigMap(int badgeType) {
        return badgeConfigMap.get(badgeType);
    }

    /**
     * 根据等级获取等级勋章
     */
    public static BadgeConfig getBadgeConfigByLevel(int badgeType, int level) {
        Map<Integer, BadgeConfig> configMap = badgeConfigMap.get(badgeType);
        if (configMap == null) {
            return null;
        }
        for (BadgeConfig badgeConfig : configMap.values()) {
            if (level == badgeConfig.getLevel()) {
                return badgeConfig;
            }
        }
        return null;
    }

}
